<template>
    <div>
        <Row type="flex" justify="start">
            <Col span="3">
                <Button type="default" long @click="backAction">返回</Button>
            </Col>
        </Row>
        <wl-excel-import
                :interface-url="interfaceUrl"
                :template-url="templateUrl"
                :uni-key="uniKey"
                :validate-data="validateData"
                :handle-data="handleData"
                :fail-column="failColumn"
        ></wl-excel-import>
    </div>
</template>

<script>
import excelTemplate from '@/libs/excel-template.js'
import { validateWhiteForeign, validateWhiteList, validateCoupon, validateBooksCoupon } from '@/libs/excel-validate.js'
import { handleWhiteList, handleCoupon, handleBooksCoupon } from '@/libs/excel-handle.js'

export default {
  data () {
    return {
      // 传入类型（用于判断处理）
      excelType: '',
      // 导入接口url
      interfaceUrl: '',
      // 导入模板地址
      templateUrl: '',
      // 格式校验方法
      validateData: () => {},
      // 数据处理方法
      handleData: () => {},
      // 失败数据的列头
      failColumn: [
        {
          key: 'name',
          title: '姓名'
        },
        {
          key: 'card_no',
          title: '身份证号'
        }
      ]
    }
  },
  beforeMount () {},
  methods: {
    init () {
      this.excelType = this.$route.query && this.$route.query.type
      switch (this.excelType) {
        case 'whiteForeign':
          this.interfaceUrl = '/api/ac/ctcm/excelService/savewhiteForeign'
          this.templateUrl = excelTemplate.whiteForeignTemplate
          this.validateData = validateWhiteForeign
          break
        case 'whiteList':
          this.interfaceUrl = '/api/ac/ctcm/excelService/savewhiteList'
          this.templateUrl = excelTemplate.whiteListTemplate
          this.validateData = validateWhiteList
          this.handleData = handleWhiteList
          break
        case 'coupon':
          this.interfaceUrl = '/api/ac/ctcm/excelService/saveNewWinList'
          this.templateUrl = excelTemplate.couponTemplate
          this.validateData = validateCoupon
          this.handleData = handleCoupon
          break
        case 'books-coupon':
          this.interfaceUrl = '/api/ac/ctcm/excelService/saveNewWinBookList'
          this.templateUrl = excelTemplate.booksCouponTemplate
          this.validateData = validateBooksCoupon
          this.handleData = handleBooksCoupon
          break
      }
    },
    backAction () {
      switch (this.excelType) {
        case 'whiteForeign':
          this.$router.push({ name: 'customerServiceWhiteForeign' })
          break
        case 'whiteList':
          this.$router.push({ name: 'customerServiceWhiteList' })
          break
        case 'coupon':
          this.$router.push({ name: 'customerServiceCoupon' })
          break
        case 'books-coupon':
          this.$router.push({ name: 'customerServiceBooksCoupon' })
          break
        default:
          this.$router.push({ name: 'index' })
          break
      }
    }
  },
  computed: {
    uniKey () {
      let repeat = ''
      switch (this.excelType) {
        case 'whiteForeign':
          repeat = 'card_no'
          break
        case 'whiteList':
          repeat = 'card_no'
          break
      }
      return repeat
    }
  },
  mounted () {
    this.init()
  }
}
</script>

<style scoped>
</style>
